﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using BLL;
using MODEL;
using System.Transactions;
using System.Data.OleDb;
namespace winUi
{
    public partial class FormRuKU : Form
    {
        private int DicId;
        private int ClassId;
        public FormRuKU()
        {
            InitializeComponent();
        }

        private void btnSearch_Click(object sender, EventArgs e)
        {
            string namePY = txtName.Text;
            dgvDic.DataSource = new BllDic().GetByPY(namePY);

        }

        private void FormRuKU_Load(object sender, EventArgs e)
        {
            dgvDic.MultiSelect = false;
            dgvDic.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            dtpDate.CustomFormat = "YYYY-MM-dd";
            dtpDate.Value = DateTime.Now.Date;
            dgvDic.AutoSizeColumnsMode = DataGridViewAutoSizeColumnsMode.DisplayedCells;
            dgvDic.MultiSelect = false;
            dgvDic.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
        }

        private void dgvDic_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && dgvDic.Focused)
            {
                DataGridViewRow row = dgvDic.CurrentRow;
                txtName.Text = row.Cells[3].Value.ToString();
                txtModel.Text = row.Cells[4].Value.ToString();
                txtSpe.Text = row.Cells[5].Value.ToString();
                txtUnit.Text = row.Cells[6].Value.ToString();
                ClassId = Convert.ToInt32(row.Cells[1].Value);
                DicId = Convert.ToInt32(row.Cells[0].Value);
                txtNumber.Focus();
            }
            return;
        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {
            string namePY = txtKey.Text;
            dgvDic.DataSource = new BllDic().GetByPY(namePY);
        }

        private void textBox1_KeyDown(object sender, KeyEventArgs e)
        {
            if (txtKey.Focused && e.KeyCode == Keys.Enter)
            {
                dgvDic.Focus();
            }
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            RuKu ruku = new RuKu();
            ruku.GoodName = txtName.Text;            
            ruku.DicId =DicId;
            ruku.Number = Convert.ToInt32(txtNumber.Text);
            ruku.Remark = txtRemark.Text;
            ruku.Price = Convert.ToDouble(txtPrice.Text);
            ruku.Date = dtpDate.Value;
            ruku.PY = dgvDic.CurrentRow.Cells[8].Value.ToString();
            ruku.ClassId =ClassId;
            try
            {
                    new ServiceRuKu().RuKu(ruku);
                    MessageBox.Show("添加成功");
                    clear();

            }
            catch (Exception ex)
            {

                MessageBox.Show(ex.Message.ToString());
            }
           
        }
        public void clear()
        {
            txtModel.Text = "";
            txtName.Text = "";
            txtNumber.Text = "0";
            txtPrice.Text = "0.00";
            txtRemark.Text = "";
            txtSpe.Text = "";
            txtUnit.Text = "";
            txtKey.Focus();
            txtKey.SelectAll();
        }

        private void txtNumber_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && txtNumber.Focused)
            {
                txtPrice.Focus();
                //txtPrice.BackColor = Color.Blue;
            }
            
        }

        private void txtPrice_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && txtPrice.Focused)
            {
                txtRemark.Focus();
                //txtRemark.BackColor = Color.Blue;
            }
        }

        private void txtRemark_KeyDown(object sender, KeyEventArgs e)
        {
            if (e.KeyCode == Keys.Enter && txtRemark.Focused)
            {
                btnSave.Focus();
                //btnSave.BackColor = Color.Blue;
            }
        }

        private void dtpDate_Enter(object sender, EventArgs e)
        {
            
        }

        private void btnAddDic_Click(object sender, EventArgs e)
        {
            new FormDic().ShowDialog();
        }
       
    }
}
